package com.handyshop.mall.user.service.impl;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.lang.tree.Tree;
import cn.hutool.core.lang.tree.TreeNode;
import cn.hutool.core.lang.tree.TreeUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.handyshop.mall.basic.core.util.SecurityUtils;
import com.handyshop.mall.dubbo.user.entity.SysMenu;
import com.handyshop.mall.dubbo.user.entity.SysPlatformMenu;
import com.handyshop.mall.dubbo.user.entity.SysRoleMenu;
import com.handyshop.mall.user.mapper.SysMenuMapper;
import com.handyshop.mall.user.service.SysMenuService;
import com.handyshop.mall.user.service.SysPlatformMenuService;
import com.handyshop.mall.user.service.SysRoleMenuService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
@RequiredArgsConstructor
public class SysMenuServiceImpl extends ServiceImpl<SysMenuMapper, SysMenu> implements SysMenuService {
    private final SysPlatformMenuService sysPlatformMenuService;
    private final SysRoleMenuService sysRoleMenuService;

    /**
     * 根据角色id查询
     *
     * @param roleId 角色id
     * @return 菜单
     */
    @Override
    public List<SysMenu> findMenuByRoleId(Long roleId) {
        LambdaQueryWrapper<SysRoleMenu> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(SysRoleMenu::getRoleId, roleId);
        List<SysRoleMenu> list = sysRoleMenuService.list(wrapper);
        if (CollUtil.isEmpty(list)) {
            return new ArrayList<>();
        }
        List<Long> menuIds = list.stream().map(SysRoleMenu::getMenuId).collect(Collectors.toList());
        return this.listByIds(menuIds);
    }

    /**
     * 根据角色ids查询
     *
     * @param roleIds 角色ids
     * @return 菜单
     */
    @Override
    public List<SysMenu> findMenuByRoleIds(List<Long> roleIds) {
        LambdaQueryWrapper<SysRoleMenu> wrapper = new LambdaQueryWrapper<>();
        wrapper.in(SysRoleMenu::getRoleId, roleIds);
        List<SysRoleMenu> list = sysRoleMenuService.list(wrapper);
        if (CollUtil.isEmpty(list)) {
            return new ArrayList<>();
        }
        List<Long> menuIds = list.stream().map(SysRoleMenu::getMenuId).collect(Collectors.toList());
        return this.listByIds(menuIds);
    }

    /**
     * 根据id删除-同时删除关联信息
     *
     * @param id 主键
     * @return true成功
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean delById(Long id) {
        boolean rst = this.removeById(id);
        if (rst) {
            LambdaQueryWrapper<SysPlatformMenu> wrapper = new LambdaQueryWrapper<>();
            wrapper.in(SysPlatformMenu::getMenuId, id);
            sysPlatformMenuService.remove(wrapper);

            LambdaQueryWrapper<SysRoleMenu> roleWrapper = new LambdaQueryWrapper<>();
            roleWrapper.in(SysRoleMenu::getMenuId, id);
            sysRoleMenuService.remove(roleWrapper);
        }
        return rst;
    }

    /**
     * 获取菜单树
     *
     * @param parentId 父级id
     * @return 树
     */
    @Override
    public List<Tree<Long>> getMenuTree(Long parentId) {
        // 构建node列表
        List<TreeNode<Long>> nodeList = CollUtil.newArrayList();
        LambdaQueryWrapper<SysMenu> wrapper = new LambdaQueryWrapper<>();
        wrapper.in(parentId != null, SysMenu::getParentId, parentId);
        List<SysMenu> sysMenus = this.list(wrapper);
        if (CollUtil.isEmpty(sysMenus)) {
            return TreeUtil.build(nodeList, 0L);
        }
        setNodeList(sysMenus, nodeList);
        return TreeUtil.build(nodeList, 0L);
    }

    /**
     * 获取菜单树
     *
     * @param menus 菜单id
     * @return 树
     */
    @Override
    public List<Tree<Long>> getMenuTree(List<Long> menus) {
        // 构建node列表
        List<TreeNode<Long>> nodeList = CollUtil.newArrayList();
        if (CollUtil.isEmpty(menus)) {
            return TreeUtil.build(nodeList, 0L);
        }
        List<SysMenu> sysMenus = this.listByIds(menus);
        if (CollUtil.isEmpty(sysMenus)) {
            return TreeUtil.build(nodeList, 0L);
        }
        setNodeList(sysMenus, nodeList);
        return TreeUtil.build(nodeList, 0L);
    }

    /**
     * 获取用户的菜单树
     *
     * @param roleId 角色id
     * @return 树
     */
    @Override
    public List<Tree<Long>> getUserMenuTree(Long roleId) {
        // 构建node列表
        List<TreeNode<Long>> nodeList = CollUtil.newArrayList();

        List<Long> roleIds = SecurityUtils.getRoles();
        if (CollUtil.isEmpty(roleIds)) {
            return TreeUtil.build(nodeList, 0L);
        }
        LambdaQueryWrapper<SysRoleMenu> wrapper = new LambdaQueryWrapper<>();
        wrapper.in(SysRoleMenu::getRoleId, roleIds)
                .eq(roleId != null, SysRoleMenu::getRoleId, roleId);
        List<SysRoleMenu> sysRoleMenus = sysRoleMenuService.list(wrapper);

        if (CollUtil.isEmpty(sysRoleMenus)) {
            return TreeUtil.build(nodeList, 0L);
        }
        List<Long> menuIds = sysRoleMenus.stream().map(SysRoleMenu::getMenuId).collect(Collectors.toList());
        List<SysMenu> sysMenus = this.listByIds(menuIds);

        setNodeList(sysMenus, nodeList);
        return TreeUtil.build(nodeList, 0L);
    }

    /**
     * 获取用户的菜单id
     *
     * @param roleId 角色id
     * @return 菜单id
     */
    @Override
    public List<Long> getUserMenuIds(Long roleId) {
        List<Long> roleIds = SecurityUtils.getRoles();
        if (CollUtil.isEmpty(roleIds)) {
            return new ArrayList<>();
        }
        LambdaQueryWrapper<SysRoleMenu> wrapper = new LambdaQueryWrapper<>();
        wrapper.in(SysRoleMenu::getRoleId, roleIds)
                .eq(roleId != null, SysRoleMenu::getRoleId, roleId);
        List<SysRoleMenu> sysRoleMenus = sysRoleMenuService.list(wrapper);

        if (CollUtil.isEmpty(sysRoleMenus)) {
            return new ArrayList<>();
        }
        return sysRoleMenus.stream().map(SysRoleMenu::getMenuId).collect(Collectors.toList());
    }

    /**
     * 组装树结构
     *
     * @param sysMenus 菜单
     * @param nodeList 树
     */
    private void setNodeList(List<SysMenu> sysMenus, List<TreeNode<Long>> nodeList) {
        for (SysMenu sysMenu : sysMenus) {
            TreeNode<Long> treeNode = new TreeNode<>(sysMenu.getId(), sysMenu.getParentId(), sysMenu.getName(), sysMenu.getSort());
            Map<String, Object> extra = new HashMap<>();
            extra.put("permission", sysMenu.getPermission());
            extra.put("path", sysMenu.getPath());
            extra.put("icon", sysMenu.getIcon());
            extra.put("component", sysMenu.getComponent());
            extra.put("status", sysMenu.getStatus());
            extra.put("type", sysMenu.getType());
            treeNode.setExtra(extra);
            nodeList.add(treeNode);
        }
    }

}
